home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / FOPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  4.1 KB  |  218 lines

  1. #ifndef TEST
  2. #include "global.h"
  3. #include "proc.h"
  4. #include "commands.h"
  5. #undef fopen
  6. #undef fclose
  7. #undef tmpfile
  8. #else
  9. #include <stdio.h>
  10. #include <string.h>
  11. #define NULLCHAR ((char *)0)
  12. #define NULLFILE ((FILE *)0)
  13. #define tcmdprintf printf
  14. #define tprintf printf
  15. #define tputs puts
  16. int dofstat (int argc,char *argv[],void *p);
  17. #endif
  18. #include <sys/stat.h>
  19.  
  20. #ifndef MSDOS
  21. #include <unistd.h>
  22. #include <time.h>
  23. #endif
  24.  
  25.  
  26. #undef DEBUG
  27. #define MAXOPENFILES    1024
  28.  
  29. static int numOpenFiles = 0;
  30. struct fileTable {
  31.     int  active;
  32. #define INACTIVE    0
  33. #define ACTIVE        1
  34.     char *name;
  35.     char *mode;
  36. #ifndef TEST
  37.     struct proc *proc;
  38. #endif
  39.     char *buf;
  40.     time_t when;
  41.     FILE *fp;
  42. };
  43.  
  44. static struct fileTable files[MAXOPENFILES];
  45.  
  46. static void register_fopen (FILE *fp, const char *fname, const char *mode);
  47. void tnosfcloseall (struct proc *p);
  48.  
  49.  
  50.  
  51. void
  52. tnosfcloseall (struct proc *p)
  53. {
  54. int k;
  55.  
  56.     for (k = 0; k < MAXOPENFILES; k++) {
  57.         if (files[k].active == ACTIVE && files[k].proc == p)    {
  58. #ifdef DEBUG
  59.             tcmdprintf ("Closing file '%s' left open by process '%s'\n",
  60.                 files[k].name, files[k].proc->name);
  61. #endif
  62.             (void) tnosfclose (files[k].fp);
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. static void
  70. register_fopen (FILE *fp, const char *fname, const char *mode)
  71. {
  72. int k;
  73.  
  74.     for (k = 0; k < MAXOPENFILES; k++) {
  75.         if (files[k].active == INACTIVE)
  76.             break;
  77.     }
  78.  
  79.     if (k == MAXOPENFILES)    {
  80. #ifdef DEBUG
  81.         tcmdprintf ("Warning: No space available to add a file to register_fopen\n");
  82. #endif
  83.     } else {
  84.         files[k].active = ACTIVE;
  85.         files[k].name = strdup (fname);
  86.         files[k].mode = strdup (mode);
  87.         files[k].fp = fp;
  88. #ifndef TEST
  89.         files[k].proc = Curproc;
  90. #endif
  91.         files[k].buf = (char *) mallocw (BUFSIZ);
  92.         (void) setvbuf (fp, files[k].buf, _IOFBF, BUFSIZ);
  93.         files[k].when = time ((time_t *)0);
  94.         numOpenFiles++;
  95.     }
  96. }
  97.  
  98.  
  99.  
  100. FILE *
  101. tnostmpfile (void)
  102. {
  103. FILE *fp;
  104.  
  105.     fp = tmpfile();
  106.     if (fp != NULLFILE)
  107.         register_fopen (fp, "tmpfile", "w+b");
  108.     return fp;
  109. }
  110.  
  111.  
  112. FILE *
  113. tnosfopen (const char *fname, const char *mode)
  114. {
  115. FILE *fp;
  116.  
  117.     fp = fopen (fname, mode);
  118.     if (fp != NULLFILE)
  119.         register_fopen (fp, fname, mode);
  120.     return fp;
  121. }
  122.  
  123. int
  124. tnosfclose (FILE *stream)
  125. {
  126. int k;
  127. int retval;
  128.  
  129.     for (k = 0; k < MAXOPENFILES; k++) {
  130.         if (files[k].active == ACTIVE && files[k].fp == stream)
  131.             break;
  132.     }
  133.     if (k == MAXOPENFILES)    {
  134. #ifdef DEBUG
  135.         tcmdprintf ("Warning: Closing a file not opened by tnosfopen\n");
  136.         tcmdprintf ("Proc = %s (%08lx)\n", Curproc->name, (long) stream);
  137. #endif
  138.     } else    {
  139.         files[k].active = INACTIVE;
  140.         free (files[k].name);
  141.         files[k].name = NULLCHAR;
  142.         free (files[k].mode);
  143.         files[k].mode = NULLCHAR;
  144.         files[k].fp = NULLFILE;
  145. #ifndef TEST
  146.         files[k].proc = NULLPROC;
  147. #endif        
  148.         numOpenFiles--;
  149.     }
  150.     retval = fclose (stream);
  151.     if (k != MAXOPENFILES)    {
  152.         free (files[k].buf);
  153.         files[k].buf = NULLCHAR;
  154.     }
  155.     return (retval);
  156. }
  157.  
  158.  
  159. int
  160. dofstat (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  161. {
  162. int heading = 0;
  163. int k;
  164. struct stat fs;
  165. char *cp;
  166.   
  167.     for (k = 0; k < MAXOPENFILES; k++) {
  168.         /* cycle through all files, quit when we reach an unused entry */
  169.         if (files[k].active == INACTIVE)
  170.             continue;
  171.         if (!heading) {
  172.             tprintf ( "\n                 Listing of Open Files: %d\n\n"
  173.                   "Process          opened at      length position mode name\n"
  174.                   "-------          ------------ -------- -------- ---- ----\n",
  175.                   numOpenFiles);
  176.             heading++;              /* header now printed */
  177.         }
  178.  
  179.         if (fstat (fileno(files[k].fp), &fs) == -1)
  180.             fs.st_size = 0;
  181.         cp = ctime(&files[k].when);
  182.         
  183.         tprintf("%-16.16s %-12.12s %8ld %8ld %-4.4s %s\n",
  184. #ifdef TEST
  185.             "current",
  186. #else
  187.             files[k].proc->name,
  188. #endif
  189.             &cp[4],    fs.st_size, ftell(files[k].fp),
  190.             files[k].mode, files[k].name);
  191.     }
  192.     tputs ("\n");
  193.     return 0;
  194. }
  195.  
  196.  
  197. #ifdef TEST
  198. void
  199. main (void)
  200. {
  201. FILE *fp1, *fp2, *fp3;
  202.  
  203.     dofstat ();
  204.     fp1 = tnosfopen ("/etc/passwd", "r");
  205.     dofstat ();
  206.     fp2 = tnosfopen ("/tmp/nerffile", "w");
  207.     dofstat ();
  208.     fp3 = tnosfopen ("/tmp/nerffile2", "a+");
  209.     dofstat ();
  210.     tnosfclose (fp1);
  211.     dofstat ();
  212.     tnosfclose (fp2);
  213.     dofstat ();
  214.     tnosfclose (fp3);
  215.     dofstat ();
  216. }
  217. #endif
  218.